package com.dd.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.*;

public class DdKeywordDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdKeywordDAO.class);

	public DdKeyword findById(java.lang.Integer id) {
		log.debug("getting DdKeyword instance with id: " + id);
		try {
			DdKeyword dk = (DdKeyword) getHibernateTemplate().get(
					"com.dd.entity.DdKeyword", id);
			return dk;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public void saveOrUpdate(DdKeyword dk) {
		log.debug("saving DdFirstPage instance");
		try {
			getHibernateTemplate().saveOrUpdate(dk);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdKeyword dk) {
		log.debug("deleting DdShop instance");
		try {
			getHibernateTemplate().delete(dk);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("findAll instance");
		try {
			return getHibernateTemplate().find("from DdKeyword");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public DdKeyword findByKeyword(String keyword, int type) {
		try {
			List li = getHibernateTemplate().find(
					"from DdKeyword where keyword='" + keyword + "' and types="
							+ type);
			if (li.size() > 0) {
				return (DdKeyword) li.get(0);
			} else {
				return null;
			}
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public List findPosByKW(String kw) {
		try {
			return getHibernateTemplate().find(
					"from DdKeyword where keyword like '%" + kw + "%' and types=0");
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public List findByKeyword(String keyword, String keyword2, String keyword3,
			int type, int count) {
		log.debug("findByType instance");
		try {
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat
					.executeQuery("select id,keyword,record from dd_keyword where keyword like'%"
							+ keyword
							+ "%' and types="
							+ type
							+ " order by hitnum desc limit " + count);
			
			String inid = "0";
			if(keyword2.equals("")){
				keyword2=keyword;
			}
			if(keyword3.equals("")){
				keyword3=keyword;
			}
			List li = new ArrayList();
			while (rs.next()) {
				List tmp = new ArrayList();
				if (inid.equals("")) {
					inid = rs.getInt("id") + "";
				} else {
					inid = inid + "," + rs.getInt("id") + "";
				}

				tmp.add(rs.getString("keyword"));
				tmp.add(rs.getInt("record"));
				li.add(tmp);
			}
			if (li.size() < count&&!keyword2.equals("")) {

				rs = stat
						.executeQuery("select id,keyword,record from dd_keyword where keyword2 like'%"
								+ keyword2
								+ "%' and types="
								+ type
								+ "  and id not in ("
								+ inid
								+ ") order by hitnum desc limit "
								+ (count - li.size()));
				
				while (rs.next()) {
					List tmp = new ArrayList();
					if (inid.equals("")) {
						inid = rs.getInt("id") + "";
					} else {
						inid = inid + "," + rs.getInt("id") + "";
					}
					tmp.add(rs.getString("keyword"));
					tmp.add(rs.getInt("record"));
					li.add(tmp);
				}
			}
			if (li.size() < count&&!keyword3.equals("")) {
				rs = stat
						.executeQuery("select id,keyword,record from dd_keyword where keyword3 like'%"
								+ keyword3
								+ "%' and types="
								+ type
								+ "  and id not in ("
								+ inid
								+ ") order by hitnum desc limit "
								+ (count - li.size()));
				while (rs.next()) {
					List tmp = new ArrayList();
					if (inid.equals("")) {
						inid = rs.getInt("id") + "";
					} else {
						inid = inid + "," + rs.getInt("id") + "";
					}
					tmp.add(rs.getString("keyword"));
					tmp.add(rs.getInt("record"));
					li.add(tmp);
				}
			}
			return li;
		} catch (Exception re) {
			System.out.println("error getKeyWord sql:"+"select id,keyword,record from dd_keyword where keyword3 like'%"
								+ keyword3
								+ "%' and types="
								+ type
								+ "  and id not in () order by hitnum desc");
			//re.printStackTrace();
			return new ArrayList();
		}
	}
}
